using System;
using NUnit.Framework;
using NHibernate.Criterion;

namespace NHibernate.Test.Playground
{
    public class WorkWith : SimpleDynamicProperties
    {
        [Test]
        public void WithSQL()
        {
            Customer customer = CreateCustomer();
            using (var session = OpenSession())
            using (var transaction = session.BeginTransaction())
            {
                session.Save(customer);
                session.Flush();
                try
                {
                    var crit = session.CreateSQLQuery("SELECT {customer.*} FROM Customers c WHERE c.FirstName = 'Fred'")
                        .AddEntity("customer", typeof(Customer));
                    var returnedCustomers = crit.List<Customer>();
                    Assert.AreEqual(1, returnedCustomers.Count);
                    var returnedCustomer = returnedCustomers[0];
                    Assert.AreEqual("Fred", returnedCustomer.FirstName);
                    Assert.AreEqual("Flintstone", returnedCustomer.DynamicProperties["LastName"]);
                }
                finally
                {
                    transaction.Rollback();
                }
            }
        }
        [Test]
        public void WithHQL()
        {
            Customer customer = CreateCustomer();
            using (var session = OpenSession())
            using (var transaction = session.BeginTransaction())
            {
                session.Save(customer);
                session.Flush();
                try
                {
                    var crit = session.CreateQuery("from Customer where FirstName = 'Fred'");
                    var returnedCustomers = crit.List<Customer>();
                    Assert.AreEqual(1, returnedCustomers.Count);
                    var returnedCustomer = returnedCustomers[0];
                    Assert.AreEqual("Fred", returnedCustomer.FirstName);
                    Assert.AreEqual("Flintstone", returnedCustomer.DynamicProperties["LastName"]);
                }
                finally
                {
                    transaction.Rollback();
                }
            }
        }
        [Test]
        public void SaveList()
        {
            Customer customer = CreateCustomer();
            using (var session = OpenSession())
            using (var transaction = session.BeginTransaction())
            {
                session.Save(customer);
                session.Flush();
                try
                {
                    var crit = session.CreateCriteria<Customer>()
                            .Add(Property.ForName("FirstName").Eq("Fred"));
                    var returnedCustomers = crit.List<Customer>();
                    Assert.AreEqual(1, returnedCustomers.Count);
                    var returnedCustomer = returnedCustomers[0];
                    Assert.AreEqual("Fred", returnedCustomer.FirstName);
                    Assert.AreEqual("Flintstone", returnedCustomer.DynamicProperties["LastName"]);
                }
                finally
                {
                    transaction.Rollback();
                }
            }
        }
        [Test]
        public void SaveAndLoadOrder()
        {
            Order order = new Order(CreateCustomer());
            order.DynamicProperties1["ProductName"] = "Tyranysaurus Eggs";
            order.DynamicProperties2["NumberOfOrders"] = 5;
            using (var session = OpenSession())
            using (var transaction = session.BeginTransaction())
            {
                session.Save(order);
                session.Flush();
                session.Evict(order);
                try
                {
                    var returnedOrder = session.Get<Order>(order.Id);
                    Assert.IsNotNull(returnedOrder);
                    Assert.AreEqual("Tyranysaurus Eggs", returnedOrder.DynamicProperties1["ProductName"]);
                    Assert.AreEqual(5, returnedOrder.DynamicProperties2["NumberOfOrders"]);
                }
                finally
                {
                    transaction.Rollback();
                }
            }
        }
        private static Customer CreateCustomer()
        {
            var customer = new Customer() { FirstName = "Fred" };
            customer.DynamicProperties.Add("LastName", "Flintstone");
            return customer;
        }
    }
}
